window.onload = function () {
    var treeObj = $("#access-tree>div").first();

    treeObj.on('changed.jstree', function (e, data) {
        var i, j, k, r = [];
        for (i = 0, j = data.selected.length; i < j; i++) {
            r.push($('#' + data.selected[i]).attr('rel'));
            //set parents node
            var parents = data.instance.get_node(data.selected[i]).parents;
            if (parents) {
                for (k = 0; k < parents.length; k++) {
                    if (parents[k].indexOf('tree-node') > -1) {
                        r.push($('#' + parents[k]).attr('rel'));
                    }
                }
            }
        }
        r = $.unique(r);
        $('#rules').val(r ? r.join(',') : '');
    }).jstree({
        "plugins": ["checkbox"],
        "checkbox": {
            "three_state": true,
            "cascade": 'up+undetermined'
        },
        "core": {
            "themes": {
                responsive: !1
            }
        }
    });
    treeObj.jstree('open_all');

    var checked = $('#rules').val().split(',');
    if (checked) {
        for (var i in checked) {
            treeObj.jstree('select_node', treeObj.attr('id') + '-' + checked[i]);
        }
    }

};

function getList(e,type) {
    $('.'+type).html('');
    var name = '';
    if(type == 'xian') {
        name = '所有区县';
        $('.town').html('');
        $('.town').append("<option value='0'>所有乡镇</option>");
        $('.village').html('');
        $('.village').append("<option value='0'>所有村</option>");
    } else if(type=='town') {
        name = '所有乡镇';
        $('.village').html('');
        $('.village').append("<option value='0'>所有村</option>");
    }
    if(e.value != 0) {
        $.ajax({
            url : areaturl,
            type : 'post',
            dataType : 'json',
            contentType : "application/json; charset=utf-8",
            data : JSON.stringify({'id':e.value}),
        }).done(function(data) {
            var $item = "<option value='0'>"+name+"</option>";
            for (let i in data){
                $item += "<option  value='"+i+"' '>"+data[i]+"</option>";
            }
            $('.'+type).append($item);           
        });
    } else {
        $('.'+type).append("<option value='0'>"+name+"</option>");
    }

}

function getVillage(e){
    $('.village').html('');
    if(e.value != 0) {
        $.ajax({
            url : villageurl,
            type : 'post',
            dataType : 'json',
            contentType : "application/json; charset=utf-8",
            data : JSON.stringify({'id':e.value}),
        }).done(function(data) {
            var $item = "<option value='0'>所有村</option>";
            for (let i in data){
                $item += "<option value='"+i+"' '>"+data[i]+"</option>";
            }
            $('.village').append($item);           
        });
    } else {
        $('.village').append("<option value='0'>所有村</option>");
    }
}

function getOrganization(e) {
    $('.orgnaztion').html('');
    if(e.value != 0) {
        $.ajax({
            url : getOrgnaztionUrl,
            type : 'post',
            dataType : 'json',
            contentType : "application/json; charset=utf-8",
            data : JSON.stringify({'villageId':e.value}),
        }).done(function(data) {
            var $item = "<option value='0'>选择组织</option>";
            for (let i in data){
                $item += "<option value='"+i+"' '>"+data[i]+"</option>";
            }
            $('.orgnaztion').append($item);
        });
    } else {
        $('.orgnaztion').append("<option value='0'>选择组织</option>");
    }
}

function getOperator() {
    alert('TODO');
}

$('#form-submit').on('click', function(){  //提交数据
    var form = this.form;
    if(form.villageId.value.trim()=='') {
        layer.msg('请选择村');
        return;
    }
    if(form.loginName.value.trim()=='') {
        layer.msg('登录名不能为空');
        form.loginName.focus();
        return;
    }
    if(form.pass_one.value.trim()=='') {
        layer.msg('请填写密码');
        form.pass_one.focus();
        return;
    }
    if(form.pass_tow.value.trim()=='') {
        layer.msg('确认密码');
        form.pass_tow.focus();
        return;
    }
    var mobile = $('#mobile').val();

    if(mobile.trim() == ''){
        layer.msg('手机号不能为空');
        form.mobile.focus();
        return;
    }
    var _data = $(form).serializeObject();
    $("#form-submit").attr("disabled","disabled");

    $.ajax({
        url : posturl,
        type : 'post',
        dataType : 'json',
        contentType : "application/json; charset=utf-8",
        data : JSON.stringify(_data),
    }).done(function(data) {
        if (data.code == 1) {
            layer.msg('保存成功');
            window.location.href= data.url; //加载页面数据
        } else if (data.code === 0 ) {  // 错误
            $("#form-submit").removeAttr("disabled");
            layer.msg(data.msg);
        }
    });
});